﻿using System.Collections.Generic;
using System.Xml;
using CeskyRozhlasRT.DataModel;
using CeskyRozhlasRT.Utils;

namespace CeskyRozhlasRT.Parsing
{
    public abstract class StationsRetriever
    {
        protected readonly IStationDescriptionXmlParser StationXmlParser = new StationDescriptionXmlParser();
        protected readonly IList<StationInfo> MStationsList = new List<StationInfo>();

        //That's how I make unit tests possible.
        //Unfortunately, in real life we use web Uri, and for unit test we need local resource.
        //XmlReader does not support "ms-appx" Uri scheme
        //Asset manager takes care of the XmlReader instantiation.
        public static IAssetManager AssetManager = new DefaultAssetManager();

        public IList<StationInfo> StationsList
        {
            get
            {
                return MStationsList;
            }
        }

    }

    public class StationListWebRetriever : StationsRetriever, IStationListRetriever
    {
        public void ParseXmlStationsList(XmlReader iReader)  
        {
            //XML parsing might be very time consuming, so let's do every station in separate worker thread.
            while (iReader.Read())
            {
                switch (iReader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (iReader.Name == "station")
                        {
                            MStationsList.Add(StationXmlParser.ParseStationDescription(iReader.ReadOuterXml()));
                        }
                        break;
                }
            }

        }

    }
}
